<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('对账单')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
    <!-- <style type="text/css">.table {table-layout:fixed;}</style> -->
</head>

<body class="gray-bg">

<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="stockHeaderForm" th:action="@{/orderHead/getByStock}" class="form-inline">
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3" style="width: 33%">
                            <label style="width: 120px">采购凭证号：</label>
                            <input type="text" name="poNo"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label style="width: 120px">供应商编码：</label>
                            <input type="text" name="sapCode"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label style="width: 120px">订单类型：</label>
                            <select name="poType" sysDict="CGDDLX" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label style="width: 120px">采购组：</label>
                            <select name="purchGroup" sysDict="CGZ" emptyFirst="true"></select>
                        </li>

                        <li class="col-sm-3" style="width: 33%">
                            <button id="stockHeaderSearchBtn" type="button" class="btn btn-primary btn-rounded btn-sm">
                                <i class="fa fa-search"></i>&nbsp;搜索
                            </button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置
                            </button>
                            <button id="stockHeaderAdvancedSearch" type="button"
                                    class="btn btn-primary btn-rounded btn-sm" style="display: none"><i
                                    class="fa fa-caret-down"></i>&nbsp;高级搜索
                            </button>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 select-info order-table">
            <div class="btn-group hidden-xs toolbar" id="stockHeaderToolBar" role="group">
                <a class="btn btn-outline btn-success btn-rounded" onclick="genBills()">
                    <i class="fa fa-edit"></i> 生成对账单
                </a>
            </div>

            <div>
                <table id="stockHeader" data-mobile-responsive="true"></table>
            </div>

        </div>
    </div>
</div>

<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<script type="text/javascript">
    $(function () {
        initDictDataSel();
    });

    //面向对象
    var headerBS = Searcher.createBS('#stockHeader',
        [
            {checkbox: true},
            {
                title: "状态", field: "billStatus",
                formatter: function (value, row, index) {
                    if (value == 5) {
                        return "对账中";
                    } else if (value == 6) {
                        return "对账成功";
                    } else if (value == -6) {
                        return "对账失败";
                    } else {
                        return "草稿";
                    }
                }
            },
            {title: "采购凭证号", field: "poNo"},
            {title: "行项目号", field: "itemNo"},
            {title: "发货单号", field: "deliverNo"},
            {title: "需求者", field: "requester"},
            {title: "收货日期", field: "stockTime"},
            {title: "收货数量", field: "shippedQty"},
            {title: '含税单价', field: 'poPrice'},
            {title: "物料编码", field: "materialCode"},
            {title: "物料描述", field: "materialDesc"},
            {title: "供应商名称", field: "vendorName"},
            {title: "订单类别", field: "poType"},
            {title: "采购组", field: "purchGroup"},
            {title: "采购组织", field: "purchOrg"},
            {title: "付款条件", field: "payCondition"},
            {title: "货币", field: "currencyCode"}
        ],{"pageList": [10, 15, 150, 200]}
    );

    //刷新数据
    function refreshHeader() {
        headerBS.refresh();
    }

    //生成对账单
    function genBills() {
        var poIds = headerBS.getSelectedRows("poId");
        if (poIds.length <= 0) {
            Dialog.errorMsg("请至少选择一行进行操作！");
            return;
        }
        var vendorCodes = headerBS.getSelectedRows("vendorCode");
        var vendorCode = vendorCodes[0];
        for (var i = 1; i < vendorCodes.length; i++) {
            if (vendorCodes[i] != vendorCode) {
                Dialog.errorMsg("所选订单不属于同一家供应商！");
                return;
            }
        }
        var rows = headerBS.getRawData();
        for (var i in rows) {
            if (rows[i].status == 5 || rows[i].status == 6 || rows[i].status == -6) {
                Dialog.errorMsg("非草稿状态，不允许对账！");
                return;
            }
        }
        var stockIds = [];
        for (var i = 0; i < rows.length; i++) {
            stockIds.push(rows[i].stockId);
        }
        Page.ajaxPOST(ctxPath + "/scmBillsHeader/checkSumMoney", {"stockIds": stockIds}, function (o) {
            if (o.code == 0) {
                var sumMoney = o.result;
                Dialog.confirm('对账金额为：' + sumMoney + '。是否确定对账？', function () {
                    Page.ajaxPOST(ctxPath + "/scmBillsHeader/genBills", {
                        "poIds": poIds,
                        "stockIds": stockIds,
                        "vendorCode": vendorCode,
                        "sumMoney": sumMoney
                    }, function (o) {
                        if (o.code == 0) {
                            Dialog.msg("成功生成对账单");
                        } else {
                            Dialog.alert("生成对账单失败!");
                        }
                        refreshHeader();//刷新表头
                    }, {"loadingMsg": "正在往服务器提交数据……"});
                });
            } else {
                Dialog.errorMsg(o.msg);
            }
        }, {"loadingMsg": "正在往服务器提交数据……"});
    }


</script>
</body>
</html>